import Vue from 'vue'
import VueRouter from 'vue-router'
//导入Login组件
import Login from '../components/Login.vue'
//导入Home组件
import Home from '../components/Home.vue'
//导入欢迎界面组件
import Welcome from '../components/Welcome.vue'
//导入用户组件
import Users from '../components/user/Users.vue'
//导入权限列表组件
import Rights from '../components/power/Rights.vue'
//导入角色列表组件
import Roles from '../components/power/Roles.vue'
//导入商品分类组件
import Cate from '../components/goods/Cate.vue'
//导入参数列表组件
import Params from '../components/goods/Params.vue'
//导入列表组件
import List from '../components/goods/List.vue'
//导入添加商品组件
import Add from '../components/goods/Add.vue'
//导入添加订单组件
import Order from '../components/order/Order.vue'
//导入添加数据组件
import Report from '../components/report/Report.vue'
Vue.use(VueRouter)

const routes = [
	// 创建路由规则,当访问/login时,显示Login组件到路由占位符
	{path:'/login',component:Login},
	{path:'/',redirect:'/login'},
	{
		path:'/home',
		component:Home,
		//先把home组件渲染出来了，然后通过路由占位符，渲染嵌套组件Welcome
		redirect:'/welcome',
		children:[
			{path:'/welcome',component:Welcome},
			{path:'/users',component:Users},
			{path:'/rights',component:Rights},
			{path:'/roles',component:Roles},
			{path:'/categories',component:Cate},
			{path:'/params',component:Params},
			{path:'/goods',component:List},
			{path:'/goods/add',component:Add},
			{path:'/orders',component:Order},
			{path:'/reports',component:Report}
		]
	}
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
	//to 将要访问的路径
	//from 代表从哪个路径跳转过来
	//next是一个函数，表示放行
	//next()放行 next('/login') 强制跳转
	if(to.path ==='/login') return next()
	//如果用户访问的不是登陆界面，则是需要获取客户端保存的token值，然后根据值判断是否登陆了
	const tokenStr=window.sessionStorage.getItem('token')
	if(!tokenStr) return next('/login')
	//运行到这里，说明token是存在的
	next()
})




export default router
